package com.zt.questionnaire.common.utils.sign;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

/**
 * AES/CBC/PKCS5Padding 对称加密
 * @author sy
 *
 */
public class AES_CBC_Utils {

	private static final String ALGORITHM = "AES/CBC/PKCS5Padding";
	          /**
	           * 数据加密
	           * @param srcData
	           * @param key
	           * @param iv
	           * @return
	           */
	          public static String encrypt(String srcData,byte[] key,byte[] iv)
	          {
	              SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
	              Cipher cipher;
	              String encodeBase64String = null;
	              try {
	                  cipher = Cipher.getInstance(ALGORITHM);
	                  cipher.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv));
	                  byte[] encData = cipher.doFinal(srcData.getBytes());
	                  encodeBase64String = Base64.getEncoder().encodeToString(encData);
	              } catch (Exception e) {
	                  e.printStackTrace();
	              }
	              return encodeBase64String;
	          }

	          /**
	           * 数据解密
	           * @param encDataStr
	           * @param key
	           * @param iv
	           * @return
	           */
	          public static String decrypt(String encDataStr,byte[] key,byte[] iv)
	          {
	              byte[] encData = Base64.getDecoder().decode(encDataStr);
	              SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
	              Cipher cipher;
	              byte[] decbbdt = null;
	              try {
	                  cipher = Cipher.getInstance(ALGORITHM);
	                  cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv));
	                  decbbdt = cipher.doFinal(encData);
	              } catch (Exception e) {
	                  e.printStackTrace();
	              }
	              return new String(decbbdt);
	          }

	      public static void main(String[] args) throws Exception {
	          String str = "fot4Ginq4iey7kLUUmA+dA==";
	          byte[] s = Base64.getDecoder().decode(str);
	          String s2 = "12345678ilkljklkjv";
	          String iv = "1234567890123456";
	          System.out.println("加密前： "+s2);
	          String encrypt = encrypt(s2, s, iv.getBytes());
	          System.out.println("加密后： "+new String(encrypt));
	          String decrypt = decrypt(encrypt, s, iv.getBytes());
	          System.out.println("解密后： "+decrypt);
	      }
}
